package health.lm.com.shop.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.SearchView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLPrivateMessageAdapter;
import health.lm.com.shop.entity.HLPrivateMessageEntity;
import health.lm.com.shop.presenter.HLPrivateMessagePresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.presenter
 * Date：2019/8/8 16:57
 * Introduce：私信
 */
@Route(path = ARouterConstant.HLPrivateMessageActivity)
public class HLPrivateMessageActivity extends XListActivity<HLPrivateMessagePresenter> {
    @BindView(R2.id.titlebar)
    CommonTitleBar mTitlebar;
    @BindView(R2.id.searchView)
    SearchView mSearchView;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout mSrlLayout;
    private HLPrivateMessageAdapter mMessageAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_people;
    }

    @Override
    public HLPrivateMessagePresenter newP() {
        return new HLPrivateMessagePresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitlebar.setListener((v, action, extra) -> finish());
        mTitlebar.getCenterTextView().setText("私信");
        mTitlebar.getLeftImageButton().setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.nav_common_icon_back_b));
        mTitlebar.getCenterTextView().setTextColor(ContextCompat.getColor(context,R.color.black));
        mSearchView.setVisibility(View.GONE);

        mMessageAdapter = new HLPrivateMessageAdapter(new ArrayList<>());
        mMessageAdapter.setOnItemClickListener((adapter1, view, position) -> {
            HLPrivateMessageEntity item = (HLPrivateMessageEntity) adapter1.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.HLChatActivity)
                    .withString("id", item.getTalk_manid())
                    .withString("name", item.getName())
                    .navigation();
        });
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        mRvList.setAdapter(mMessageAdapter);
        super.recyclerView = mRvList;
        super.rlRefreshLayout = mSrlLayout;
        super.adapter = mMessageAdapter;
        super.initData(savedInstanceState);
        //getP().getBusinessListData(page, pageSize);
        getP().getData(page, pageSize);
    }

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getData(page, pageSize);
    }

    public void setNewData(List<HLPrivateMessageEntity> pNewData) {
        if (isRefresh) {
            mSrlLayout.finishRefresh();
            mMessageAdapter.setNewData(pNewData);
        } else {
            mMessageAdapter.addData(pNewData);
            if (pNewData.size() >= pageSize) mMessageAdapter.loadMoreComplete();
            else mMessageAdapter.loadMoreEnd();
        }
    }
}
